<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

class Admin extends \app\BaseController
{
    protected $model;
    protected $config_model;
    protected $uniacid;
    protected $_uniacid;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\massage\model\Admin();
        $this->uniacid = 666;
        $this->_uniacid = 666;
    }
    public function getW7TmpV2()
    {
        $copyright = getConfigSetting(666, "copyright");
        if(!empty($copyright)) {
            $arr["w7tmp"]["footerleft"] = "<div> " . $copyright . " </div>";
        } else {
            $arr["w7tmp"] = 1;
        }
        return $this->success($arr);
    }
    public function authPhone()
    {
        $phone = getConfigSetting(666, "login_phone_auth");
        $auth = $phone == 1 && !cookie("getAuthPhone") ? 1 : 0;
        return $this->success($auth);
    }
    public function sendAuthCode()
    {
        $input = json_decode($this->request->getInput(), true);
        $dis = ["status" => 1, "username" => $input["username"], "passwd" => checkPass($input["passwd"])];
        $data = $this->model->dataInfo($dis);
        if(empty($data)) {
            return $this->error("密码错误", 400);
        }
        if($data["is_admin"] == 1) {
            $phone = getConfigSetting($data["uniacid"], "login_auth_phone");
        } else {
            $phone = $data["login_auth_phone"];
        }
        if(empty($phone)) {
            return $this->error("未获取到授权手机号，请联系开发平台", 400);
        }
        $config_model = new \app\massage\model\ShortCodeConfig();
        $res = $config_model->loginShortConfig($phone, $data["uniacid"]);
        if(!empty($res["Message"]) && $res["Message"] == "OK") {
            return $this->success($phone);
        }
        return $this->error($res["Message"]);
    }
    public function sendAuthCodeV2()
    {
        $input = json_decode($this->request->getInput(), true);
        $phone = getConfigSetting($this->_uniacid, "login_auth_phone");
        if(!empty($input["phone"])) {
            $phone = $input["phone"];
        }
        if(empty($phone)) {
            return $this->error("未获取到授权手机号，请联系开发平台", 400);
        }
        $config_model = new \app\massage\model\ShortCodeConfig();
        $res = $config_model->loginShortConfigV2($phone, $this->_uniacid);
        if(!empty($res["Message"]) && $res["Message"] == "OK") {
            return $this->success($phone);
        }
        return $this->error($res["Message"]);
    }

    public function login()
    {
        initLogin();
        $input = json_decode($this->request->getInput(), true);
        $codeText = cookie("codeText");
        $phone_auth = getConfigSetting(666, "login_phone_auth");
        $auth = $phone_auth == 1 && !cookie("getAuthPhone") ? 1 : 0;
        if($auth == 0 && $codeText != $input["codeText"]) {
            return $this->error("验证码错误");
        }
        $ip = getIP();
        $key = $ip . "errss_numsss";
        $err_num = getCache($key, 66661);
        $err_num = !empty($err_num) ? $err_num : 0;
        if(5 <= $err_num) {
            return $this->error("密码错误超过5次，请2小时后再试", 400);
        }
        $err_num += 1;
        setCache($key, $err_num, 7200, 66661);
        $dis = ["status" => 1, "username" => $input["username"], "passwd" => checkPass($input["passwd"])];
        $data = $this->model->dataInfo($dis);
        $login_num = 5 - $err_num;
        if(empty($data)) {
            if($login_num <= 0) {
                return $this->error("密码错误超过5次，请2小时后再试", 400);
            }
            return $this->error("账号密码错误，你还剩" . $login_num . "次机会", 400);
        }
        if($auth == 1) {
            $short_phone = $data["is_admin"] == 1 ? getConfigSetting(666, "login_auth_phone") : $data["login_auth_phone"];
            $short_code = getCache($short_phone . "login", $data["uniacid"]);
            if(empty($input["short_code"]) || $short_code != $input["short_code"]) {
                return $this->error("短信验证码错误", 400);
            }
            cookie("getAuthPhone", 1, 2592000);
            setCache($short_phone . "login", "", 99, $data["uniacid"]);
        }
        setCache($key, 0, 7200, 66661);
        $result["user"] = $data;
        $result["token"] = uuid();
        if(empty($result["token"])) {
            return $this->error("系统错误", 400);
        }
        setUserForToken($result["token"], $data);
        $this->addActionLog($data["id"]);
        return $this->success($result);
    }
    public function addActionLog($user_id)
    {
        $insert = ["uniacid" => 666, "user_id" => $user_id, "obj_id" => 0, "ip" => getIP(), "model" => "Admin", "method" => "POST", "table" => "shequshop_school_admin", "code_action" => "login", "action_type" => "", "action" => "login"];
        $log_model = new \app\massage\model\ActionLog();
        $log_model->dataAdd($insert);
        return true;
    }
    public function success($data, $code = 200)
    {
        $result["data"] = $data;
        $result["code"] = $code;
        $result["sign"] = NULL;
        if(!empty($this->_token)) {
            $result["sign"] = createSimpleSign($this->_token, is_string($data) ? $data : json_encode($data));
        }
        return $this->response($result, "json", $code);
    }
    public function error($msg, $code = 400)
    {
        $result["error"] = \think\facade\Lang::get($msg);
        $result["code"] = $code;
        return $this->response($result, "json", 200);
    }
    protected function response($data, $type = "json", $code = 200)
    {
        return \think\Response::create($data, $type)->code($code);
    }
    public function getConfig()
    {
        $config_model = new \app\massage\model\Config();
        $config = $config_model->dataInfo(["uniacid" => 666]);
        $data["record_type"] = $config["record_type"];
        $data["record_no"] = $config["record_no"];
        $data["login_des"] = getConfigSetting(666, "login_des");
        return $this->success($data);
    }
    public function excel()
    {
    }
    public function checkipaddres($ipaddres)
    {
        $preg = "/\\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\Z/";
        if(preg_match($preg, $ipaddres)) {
            return true;
        }
        return false;
    }
    public function getMapCoach()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", 666];
        $dis[] = ["status", "=", 2];
        if(!empty($input["city_id"])) {
            $dis[] = ["city_id", "=", $input["city_id"]];
        }
        $coach_model = new \app\massage\model\Coach();
        $data = $coach_model->where($dis)->field("id as coach_id,lng,lat,coach_name,work_img")->select()->toArray();
        return $this->success($data);
    }
    public function citySelect()
    {
        $input = $this->_param;
        $city_model = new \app\massage\model\City();
        $dis[] = ["uniacid", "=", 666];
        $dis[] = ["status", "=", 1];

        $data = $city_model->where($dis)->where(function ($query) {
            $mapor = ["city_type" => 1, "is_city" => 1];
            $query->whereOr($mapor);
        })->field(["id,title,lat,lng"])->order("id desc")->select()->toArray();
        return $this->success($data);
    }
    public function coachInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $coach_model = new \app\massage\model\Coach();
        $data = $coach_model->where($dis)->withoutField("id_card,id_code,mobile,service_price")->find()->toArray();
        $config_model = new \app\massage\model\Config();
        $config = $config_model->dataInfo(["uniacid" => $this->uniacid]);
        $data["text_type"] = $coach_model->getCoachWorkStatus($input["id"], $this->uniacid);
        $data["near_time"] = $coach_model->getCoachEarliestTime($input["id"], $config);
        $data["order_num"] += $data["total_order_num"];
        $type_model = new \app\industrytype\model\Type();
        $data["industry_data"] = $type_model->dataInfo(["id" => $data["industry_type"]]);
        return $this->success($data);
    }
    public function orderDataaa()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["pay_time", ">", 0];
        $dis[] = ["pay_type", ">", 1];
        $order_model = new \app\massage\model\Order();
        if($input["day"] == 4) {
            $start = strtotime(date("Y-01"));
            $i = 0;
            while ($i < 12) {
                $arr[$i]["month"] = $i + 1;
                $arr[$i]["time"] = $start;
                $time_text = date("Y-m", $start);
                $arr[$i]["time_text"] = $time_text;
                $shop_price = $order_model->where($dis)->whereMonth("create_time", $time_text)->sum("true_service_price");
                $arr[$i]["shop_price"] = round($shop_price, 2);
                $i++;
                $start = strtotime($time_text . " +1 month");
            }
        } else {
            if($input["day"] == 5) {
                $start = $input["start_time"];
                $end = $input["end_time"];
            } else {
                if($input["day"] == 1) {
                    $time = 1;
                } elseif($input["day"] == 2) {
                    $time = 7;
                } else {
                    $time = 30;
                }
                $end = strtotime(date("Y-m-d", time()));
                $start = $end - ($time - 1) * 86400;
            }
            for ($i = 0; $start <= $end; $i++) {
                $arr[$i]["time"] = $start;
                $time_text = date("Y-m-d", $start);
                $arr[$i]["time_text"] = date("m-d", $start);
                $shop_price = $order_model->where($dis)->whereDay("create_time", $time_text)->sum("true_service_price");
                $arr[$i]["shop_price"] = round($shop_price, 2);
                $start += 86400;
            }
        }
        return $this->success($arr);
    }
    public function dataScreenaa()
    {
        exit;
    }
    public function policeList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.status", ">", -1];
        if(isset($input["have_look"])) {
            $dis[] = ["a.have_look", "=", $input["have_look"]];
        }
        if(!empty($input["start_time"])) {
            $start_time = $input["start_time"];
            $end_time = $input["end_time"];
            $dis[] = ["a.create_time", "between", $start_time . "," . $end_time];
        }
        $police_model = new \app\massage\model\Police();
        $data = $police_model->noLogindataList($dis, $input["limit"]);
        return $this->success($data);
    }
    public function drawCashInfo()
    {
        $wallet = new \app\massage\model\Wallet();
        $adapay = new \longbingcore\wxcore\Adapay($this->_uniacid);
        $list = $wallet->where(["status" => 4, "online" => 4])->order("sh_time,id")->limit(10)->select()->toArray();
        if(!empty($list)) {
            foreach ($list as $value) {
                $find = $adapay->drawCashInfo($value["adapay_code"]);
                if(!empty($find) && $find["status"] == "succeeded" && $find["cash_list"][0]["trans_stat"] == "S") {
                    $wallet->dataUpdate(["id" => $value["id"]], ["status" => 2]);
                }
            }
        }
        return $this->success(true);
    }
}

?>